﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DAO;
using PMQLCHH.DTO;


namespace PMQLCHH.GUI
{
    public partial class CHITIETHOADON : DevComponents.DotNetBar.Office2007Form
    {
        //Kha Nguyen - Giao Dien chi tiet hoa don - 5/6/2013 - begin
        public CHITIETHOADON(string MaHD)
        {
            InitializeComponent();
            strMaHD = MaHD;
        }

        //Kha Nguyen - bien luu so luong truoc khi sua
        private int m_iSoLuongTruoc;
        //Kha Nguyen - bien luu Gia Ban truoc khi sua
        private long m_lGiaBanTruoc;
        private string strMaHD;
        //Quang Dat Them Cho Kha Phan PopUp Khach Hàng 08/06/2013 10h:24 PM

 
        
        public void GetValue(string makhachhang, string tenkhachhang, string dienthoai, string diachi, string masothue)
        {
            txt_MaKhachHang.Text = makhachhang;
            txt_TenKhachHang.Text = tenkhachhang;
            txt_DTKhachHang.Text = dienthoai;
            txt_DCKhachHang.Text = diachi;
            txtMaSoThue.Text = masothue;

        }

        private void btn_TimKhachHang_Click(object sender, EventArgs e)
        {
            POPUPKHACHHANG frm_popupkh = new POPUPKHACHHANG();
            frm_popupkh.get_MaKH = new POPUPKHACHHANG.GetString(GetValue);
            frm_popupkh.ShowDialog();
        }

        //Kha Nguyen - 10/6/2013 - begin
        private void btn_ThemMatHang_Click(object sender, EventArgs e)
        {
            POPUPMATHANG frm_popupmh = new POPUPMATHANG();
            frm_popupmh.get_MaMH = new POPUPMATHANG.GetArray(LayDanhSachMaMatHang);
            frm_popupmh.ShowDialog();
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LAPHOADON_Load(object sender, EventArgs e)
        {
            NapThamSoMatDinh();
        }


        //cap nhat so luong hoac cap nhat gia ban
        private void dtg_DanhSachMatHang_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dtg_DanhSachMatHang.Rows.Count != 0)
            {
                int iDong = e.RowIndex;
                int iCot = e.ColumnIndex;
                if (iCot == 5)//neu dang sua cot So Luong
                {
                    bool result = false;
                    int iSoLuong = 0;
                    result = int.TryParse(dtg_DanhSachMatHang.Rows[iDong].Cells["SoLuong"].Value.ToString(), out iSoLuong);
                    if (!result)
                    {
                        dtg_DanhSachMatHang.Rows[iDong].Cells["SoLuong"].Value = m_iSoLuongTruoc;
                    }
                    else
                    {
                        //string MaMH = dtg_DanhSachMatHang.Rows[iDong].Cells["MaMatHang"].Value.ToString();

                        //int SoLuongTon = 0;
                        //MatHangBUS MHBus = new MatHangBUS();
                        //DataTable dt = new DataTable();
                        //dt = MHBus.LayChiTietMatHang(MaMH);
                        //if (dt.Rows.Count != 0)
                        //{
                        //    SoLuongTon = int.Parse(dt.Rows[0]["SoLuongTon"].ToString());
                        //    SoLuongTon = CapNhatSoLuongTon(MaMH, SoLuongTon);
                        //}
                        //if (SoLuongTon >= 0)
                        //{
                        long GiaBan = long.Parse(dtg_DanhSachMatHang.Rows[iDong].Cells["GiaBan"].Value.ToString());
                        long TongTien = GiaBan * iSoLuong;
                        dtg_DanhSachMatHang.Rows[iDong].Cells["ThanhTien"].Value = TongTien;
                        CapNhatTongTienVaConNo();
                        //}
                        //else
                        //{
                        //    MessageBox.Show("Số lượng tồn không đủ!","Thông Báo");
                        //    dtg_DanhSachMatHang.Rows[iDong].Cells["SoLuong"].Value = m_iSoLuongTruoc;
                        //}
                    }
                }
                else
                {
                    if (iCot == 6)//neu dang sua cot Gia Ban
                    {
                        bool result = false;
                        long GiaBan = 0;
                        result = long.TryParse(dtg_DanhSachMatHang.Rows[iDong].Cells["GiaBan"].Value.ToString(), out GiaBan);
                        if (!result)
                        {
                            dtg_DanhSachMatHang.Rows[iDong].Cells["GiaBan"].Value = m_lGiaBanTruoc;
                        }
                        else
                        {
                            int iSoLuong = int.Parse(dtg_DanhSachMatHang.Rows[iDong].Cells["SoLuong"].Value.ToString());
                            long TongTien = GiaBan * iSoLuong;
                            dtg_DanhSachMatHang.Rows[iDong].Cells["ThanhTien"].Value = TongTien;
                            CapNhatTongTienVaConNo();
                        }
                    }
                }
            }
        }



        //Xoa 1 Mat Hang
        private void dtg_DanhSachMatHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DaBamNutXoa(e))
            {
                //xoa
                int iDong = e.RowIndex;
                DataGridView dt = new DataGridView();
                dt = dtg_DanhSachMatHang;
                dt.Rows.RemoveAt(iDong);
                int i = 0;
                foreach (DataGridViewRow dr in dt.Rows)
                {
                    dtg_DanhSachMatHang.Rows[i].Cells["STT"].Value = i + 1;
                    i++;
                }
                CapNhatTongTienVaConNo();
            }
        }

        //Luu bien truoc khi chinh sua
        private void dtg_DanhSachMatHang_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int iDong = e.RowIndex;
            int iCot = e.ColumnIndex;
            if (iCot == 5)
            {
                m_iSoLuongTruoc = int.Parse(dtg_DanhSachMatHang.Rows[iDong].Cells["SoLuong"].Value.ToString());
            }
            else
            {
                if (iCot == 6)
                {
                    m_lGiaBanTruoc = long.Parse(dtg_DanhSachMatHang.Rows[iDong].Cells["GiaBan"].Value.ToString());
                }
            }
            
        }

        private void txt_TraTruoc_TextChanged(object sender, EventArgs e)
        {
            KiemTraTraTruoc();
        }

        private void tbn_LamMoi_Click(object sender, EventArgs e)
        {
            NapThamSoMatDinh();
        }

        private void btn_LapHoaDon_Click(object sender, EventArgs e)
        {
            CapNhatHoaDon();
        }

        private void txt_VAT_TextChanged(object sender, EventArgs e)
        {
            KiemTraVAT();
        }

        private void txt_PhiVanChuyen_TextChanged(object sender, EventArgs e)
        {
            KiemTraPhiVanChuyen();
        }


        //Kha Nguyen - 10/6/2013 - end
        //event 
        


        //methods
        /// <summary>
        /// kiem tra phan duoc bam vao la 1 hinh anh (Nut xoa)
        /// </summary>
        ///
        private bool DaBamNutXoa(DataGridViewCellEventArgs cellEvent)
        {
            if (dtg_DanhSachMatHang.Columns[cellEvent.ColumnIndex] is DataGridViewImageColumn && cellEvent.RowIndex != -1)
            { 
                return true; 
            }
            else 
            { 
                return false; 
            }
        }

        /// <summary>
        /// Lay danh sach mat hang da chon
        /// </summary>
        ///
        public void LayDanhSachMaMatHang(ArrayList ArrDSMH)
        {
            try
            {
                MatHangBUS MHBus = new MatHangBUS();
                foreach (object obj in ArrDSMH)
                {
                    DataTable dt = new DataTable();
                    string MaMH = obj.ToString();

                    dt = MHBus.LayChiTietMatHang(MaMH);
                    if (dt.Rows.Count != 0)
                    {
                        //int SLT = int.Parse(dt.Rows[0]["SoLuongTon"].ToString());
                        //SLT = KiemTraSoLuongTon(MaMH, SLT, 1);
                        //if (SLT >= 0)
                        //{
                            dtg_DanhSachMatHang.Rows.Add(dtg_DanhSachMatHang.Rows.Count + 1, MaMH, dt.Rows[0]["TenMatHang"].ToString(), dt.Rows[0]["TenLoaiMatHang"].ToString(), dt.Rows[0]["TenDonVi"].ToString(), 1, dt.Rows[0]["GiaThamKhao"].ToString(), dt.Rows[0]["GiaThamKhao"].ToString(), (System.Drawing.Image)Properties.Resources.Close_16);
                        //}
                        //else
                        //{
                        //    MessageBox.Show("Số lượng tồn không đủ!", "Thông Báo");
                        //}
                    }
                }
                CapNhatTongTienVaConNo();
            }
            catch (Exception ex)
            {

            }

        }


        /// <summary>
        /// Kiem tra So Luong Ton khi them 1 mat hang
        /// </summary>
        ///
        public int KiemTraSoLuongTon(string MaMH, int SLT, int SL)
        {
            int Count = 0;
            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                string Temp = dr.Cells["MaMatHang"].Value.ToString();
                if (String.Compare(Temp, MaMH) == 0)
                {
                    int SoLuongMua = int.Parse(dr.Cells["SoLuong"].Value.ToString());
                    Count += SoLuongMua;
                }
            }
            int SoLTDong = SLT;
            SoLTDong = SoLTDong - Count - SL;
            //foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            //{

            //    string Temp = dr.Cells["MaMatHang"].Value.ToString();
            //    if (String.Compare(Temp, MaMH) == 0)
            //    {
            //        dr.Cells["SoLuongTon"].Value = SoLTDong;
            //    }
            //}
            return SoLTDong;
        }

        /// <summary>
        /// Cap Nhat Lai So Luong Ton khi sua 1 mat hang
        /// </summary>
        ///
        public int CapNhatSoLuongTon(string MaMH, int SLT)
        {
            int Count = 0;
            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                string Temp = dr.Cells["MaMatHang"].Value.ToString();
                if (String.Compare(Temp, MaMH) == 0)
                {
                    int SoLuongMua = int.Parse(dr.Cells["SoLuong"].Value.ToString());
                    Count += SoLuongMua;
                }
            }
            int SoLTDong = SLT;
            SoLTDong = SoLTDong - Count;

            return SoLTDong;
        }

 

        /// <summary>
        /// Nap Trang Thai
        /// </summary>
        ///
        public void NapDanhSachTrangThai()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");

            //them gia tri
            DataRow newCustomersRow = dt.NewRow();
            newCustomersRow["Text"] = "Chưa Thanh Toán";
            newCustomersRow["Value"] = 0;
            dt.Rows.Add(newCustomersRow);

            newCustomersRow = dt.NewRow();
            newCustomersRow["Text"] = "Đã Thanh Toán";
            newCustomersRow["Value"] = 1;
            dt.Rows.Add(newCustomersRow);
 
            cbb_TrangThai.DataSource = dt;
            cbb_TrangThai.ValueMember = "Value";
            cbb_TrangThai.DisplayMember = "Text";
            cbb_TrangThai.SelectedIndex = 0;
        }




        /// <summary>
        /// Nap Thong So Mat Dinh
        /// </summary>
        ///
        public void NapThamSoMatDinh()
        {
           
            NapDanhSachTrangThai();
            dtg_DanhSachMatHang.Rows.Clear();
            NapDanhSachMatHangTrongHoaDon();
            DaHetHang();
            HoaDonBUS HDBus = new HoaDonBUS();
            
            HoaDonDTO HD = new HoaDonDTO();
            HD = HDBus.LayHoaDon(strMaHD);
            KhachHangDTO KH = new KhachHangDTO();
            KH = KhachHangBUS.LayThongTinKhachHang(HD.StrMaKhachHang);
            lbl_MaHoaDon.Text = HD.StrMaHoaDon;
            cbb_TrangThai.SelectedValue = HD.ITrangThai;
            //cbb_GiaoHang.SelectedValue = HD.IGiaoHang;
            if (HD.ITrangThai == 0)
            {
                lbl_TrangThaiHienTai.Text = "Chưa thanh toán";
            }
            else
            {
                lbl_TrangThaiHienTai.Text = "Đã thanh toán";
                btn_TimKhachHang.Enabled = false;
                btn_ThemMatHang.Enabled = false;
                txt_TenNguoiNhan.Enabled = false;
                txtDTNguoiNhan.Enabled = false;
                txtGiaoDen.Enabled = false;
                txt_VAT.Enabled = false;
                txt_PhiVanChuyen.Enabled = false;
                txt_TraTruoc.Enabled = false;
                txt_TongTien.Enabled = false;
                txt_GhiChu.Enabled = false;
                txt_ConNo.Enabled = false;
                dtp_NgayGiao.Enabled = false;
                cbb_TrangThai.Enabled = false;
                dtg_DanhSachMatHang.Enabled = false;
                btn_CapNhatHoaDon.Enabled = false;
                btn_lammoi.Enabled = false;
                txt_DCKhachHang.Enabled = false;
                txt_MaKhachHang.Enabled = false;
                txt_TenKhachHang.Enabled = false;
                txtMaSoThue.Enabled = false;
                txt_DTKhachHang.Enabled = false;
                //if (HD.IGiaoHang == 1)
                //{
                //    cbb_GiaoHang.Enabled = false;
                //    btn_LapHoaDon.Enabled = false;
                //    btn_lammoi.Enabled = false;
                //}
            }

            txt_VAT.Text = HD.IVat.ToString();
            txt_PhiVanChuyen.Text = HD.FPhiVanChuyen.ToString();
            txt_TraTruoc.Text = HD.FTraTruoc.ToString();
            txt_ConNo.Text = HD.FConNo.ToString();
            txt_TongTien.Text = HD.FTongTien.ToString();
            txt_DCKhachHang.Text = KH.Diachi;
            txt_DTKhachHang.Text = KH.Sodienthoai;
            txt_GhiChu.Text = HD.StrGhiChu;
            txt_MaKhachHang.Text = KH.Makhachhang;
            txt_TenKhachHang.Text = KH.Tenkhachhang;
            txt_TenNguoiNhan.Text = HD.StrTenNguoiNhan;
            txtDTNguoiNhan.Text = HD.StrSoDienThoaiNguoiNhan;
            txtGiaoDen.Text = HD.StrDiaChiGiao;
            txtMaSoThue.Text = KH.Masothue;

            dtp_NgayGiao.Format = DateTimePickerFormat.Custom;
            dtp_NgayGiao.CustomFormat = "dd/MM/yyyy";
            dtp_NgayGiao.Value = DateTime.Parse(HD.DtNgayGiao);

            //CapNhatTongTienVaConNo();
            
        }


        /// <summary>
        /// Lay danh sach mat hang da co trong hoa don
        /// </summary>
        ///

        public void NapDanhSachMatHangTrongHoaDon()
        {
            HoaDonBUS HDBus = new HoaDonBUS();
            MatHangBUS MHBus = new MatHangBUS();
            DataTable dt = new DataTable();
            dt = HDBus.LayDSMHChiTietHoaDon(strMaHD);
            //dtg_DanhSachMatHang.DataSource = dt;
            foreach (DataRow dr in dt.Rows)
            {
                if (MHBus.MaMatHangDaBat(dr["MaMatHang"].ToString()))
                {
                    dtg_DanhSachMatHang.Rows.Add(dtg_DanhSachMatHang.Rows.Count + 1, dr["MaMatHang"].ToString(), dr["TenMatHang"].ToString(), dr["TenLoaiMatHang"].ToString(), dr["TenDonVi"].ToString(), dr["SoLuong"].ToString(), dr["GiaBan"].ToString(), dr["ThanhTien"].ToString(), (System.Drawing.Image)Properties.Resources.Close_16);
                }
                else
                {
                    dtg_DanhSachMatHang.Rows.Add((dtg_DanhSachMatHang.Rows.Count + 1), dr["MaMatHang"].ToString(), dr["TenMatHang"].ToString(), dr["TenLoaiMatHang"].ToString(), dr["TenDonVi"].ToString(), dr["SoLuong"].ToString(), dr["GiaBan"].ToString(), dr["ThanhTien"].ToString(), (System.Drawing.Image)Properties.Resources.Close_16);
                    dtg_DanhSachMatHang.Rows[dtg_DanhSachMatHang.Rows.Count - 1].Cells["MaMatHang"].Style.SelectionForeColor = Color.Red;
                    dtg_DanhSachMatHang.Rows[dtg_DanhSachMatHang.Rows.Count - 1].Cells["MaMatHang"].Style.ForeColor = Color.Red;
                    dtg_DanhSachMatHang.Rows[dtg_DanhSachMatHang.Rows.Count - 1].Cells["SoLuong"].ReadOnly = true;
                    dtg_DanhSachMatHang.Rows[dtg_DanhSachMatHang.Rows.Count - 1].Cells["GiaBan"].ReadOnly = true;
                }
            }
        }


        /// <summary>
        /// Tinh Tong Tien
        /// </summary>
        ///
        public long TinhTongTien()
        {
            long TongTien = 0;
            DataGridView dgv = new DataGridView();
            dgv = dtg_DanhSachMatHang;
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                long lTongTien = long.Parse(dr.Cells["ThanhTien"].Value.ToString());
                TongTien += lTongTien;
            }
            int iVAT = 0;
            iVAT = int.Parse(txt_VAT.Text);
            long lPhiVanChuyen = 0;
            lPhiVanChuyen = long.Parse(txt_PhiVanChuyen.Text);
            TongTien += (TongTien * iVAT / 100) + lPhiVanChuyen;
            return TongTien;
        }

        /// <summary>
        /// Cap Nhat Lai Tong Tien Va Con No
        /// </summary>
        ///
        public void CapNhatTongTienVaConNo()
        {
            long TongTien = 0;
            TongTien = TinhTongTien();
            txt_TongTien.Text = TongTien.ToString();
            long ConNo = 0;
            long TraTruoc = 0;
            TraTruoc = long.Parse(txt_TraTruoc.Text);
            ConNo = TongTien - TraTruoc;
            if (ConNo > 0)
            {
                txt_ConNo.Text = ConNo.ToString();
            }
            else
            {
                txt_ConNo.Text = "0";
            }
        }

        /// <summary>
        /// Kiem Tra Tra Truoc
        /// </summary>
        ///
        public void KiemTraTraTruoc()
        {
            bool result = false;
            long TraTruoc = 0;
            result = long.TryParse(txt_TraTruoc.Text, out TraTruoc);
            if (!result)
            {
                MessageBox.Show("Trả trước nhập sai định dạng! \nVui lòng chỉ nhập số", "Thông Báo");
            }

            txt_TraTruoc.Text = TraTruoc.ToString();
            CapNhatTongTienVaConNo();
            
        }



        /// <summary>
        /// Kiem Tra VAT
        /// </summary>
        ///
        public void KiemTraVAT()
        {
            bool result = false;
            int VAT = 0;
            result = int.TryParse(txt_VAT.Text, out VAT);
            if (!result)
            {
                MessageBox.Show("VAT nhập sai định dạng! \nVui lòng chỉ nhập số", "Thông Báo");
            }

            txt_VAT.Text = VAT.ToString();
            CapNhatTongTienVaConNo();

        }

        /// <summary>
        /// Kiem Tra Phi Van Chuyen
        /// </summary>
        ///
        public void KiemTraPhiVanChuyen()
        {
            bool result = false;
            long PhiVanChuyen = 0;
            result = long.TryParse(txt_PhiVanChuyen.Text, out PhiVanChuyen);
            if (!result)
            {
                MessageBox.Show("Phí vận chuyển nhập sai định dạng! \nVui lòng chỉ nhập số", "Thông Báo");
            }

            txt_PhiVanChuyen.Text = PhiVanChuyen.ToString();
            CapNhatTongTienVaConNo();

        }

        /// <summary>
        /// Lap Hoa Don
        /// </summary>
        ///

        public void CapNhatHoaDon()
        {
            if (String.Compare(txt_MaKhachHang.Text, "") != 0)
            {
                if (dtg_DanhSachMatHang.Rows.Count != 0)
                {
                    HoaDonDTO HD = new HoaDonDTO();
                    HD.StrMaHoaDon = lbl_MaHoaDon.Text;
                    HD.DtNgayGiao = dtp_NgayGiao.Value.Year + "/" + dtp_NgayGiao.Value.Month + "/" + dtp_NgayGiao.Value.Day;
                    HD.FConNo = long.Parse(txt_ConNo.Text);
                    HD.FPhiVanChuyen = long.Parse(txt_PhiVanChuyen.Text);
                    HD.FTongTien = long.Parse(txt_TongTien.Text);
                    HD.FTraTruoc = long.Parse(txt_TraTruoc.Text);
                    //HD.IGiaoHang = int.Parse(cbb_GiaoHang.SelectedValue.ToString());
                    HD.ITrangThai = int.Parse(cbb_TrangThai.SelectedValue.ToString());
                    HD.IVat = int.Parse(txt_VAT.Text);
                    HD.StrDiaChiGiao = txtGiaoDen.Text;
                    HD.StrGhiChu = txt_GhiChu.Text;
                    HD.StrMaKhachHang = txt_MaKhachHang.Text;
                    HD.StrSoDienThoaiNguoiNhan = txtDTNguoiNhan.Text;
                    HD.StrTenNguoiNhan = txt_TenNguoiNhan.Text;



                    HoaDonBUS HDBus = new HoaDonBUS();


                    bool flag = true;
                    if (HD.ITrangThai == 1 && DaHetHang())
                    {
                            MessageBox.Show("Số lượng tồn không đủ để thực hiện giao dịch!\nHoặc mặt hàng không tồn tại!", "Thông Báo");
                            flag = false;
                    }

                    //if (HD.ITrangThai == 1 && DaXoaHang())
                    //{
                    //    MessageBox.Show("Có 1 vài mặt hàng không tồn tại nên không thể thực hiện giao dịch!", "Thông Báo");
                    //    flag = false;
                    //}

                    if ((flag) && DeDenHan(HD.StrMaHoaDon))
                    {
                        DialogResult ms = MessageBox.Show("Chưa đến ngày giao! bạn có chắc muốn thanh toán hóa đơn này?", "Thông Báo", MessageBoxButtons.YesNo);
                        if (ms == DialogResult.Yes)
                        {
                            flag = true;
                        }
                        else if (ms == DialogResult.No)
                        {
                            flag = false;
                        }
                    }

                    if (flag)
                    {
                            bool KQ = false;
                            KQ = HDBus.CapNhatHoaDon(HD);
                            if (!KQ)
                            {
                                //Cap Nhat That bai
                            }
                            else
                            {
                                HDBus.XoaCTHoaDon(HD.StrMaHoaDon);
                                DataGridView drv = new DataGridView();
                                drv = dtg_DanhSachMatHang;
                                foreach (DataGridViewRow dr in drv.Rows)
                                {
                                    ChiTietHoaDonDTO CTHD = new ChiTietHoaDonDTO();
                                    CTHD.FGiaBan = long.Parse(dr.Cells["GiaBan"].Value.ToString());
                                    CTHD.FThanhTien = long.Parse(dr.Cells["ThanhTien"].Value.ToString());
                                    CTHD.ISoLuong = int.Parse(dr.Cells["SoLuong"].Value.ToString());
                                    CTHD.ITrangThai = 1;
                                    CTHD.StrMaHoaDon = HD.StrMaHoaDon;
                                    CTHD.StrMaMatHang = dr.Cells["MaMatHang"].Value.ToString();
                                    HDBus.ThemChiTietHoaDon(CTHD);
                                }
                            }

                            if (HD.ITrangThai == 1)
                            {
                                HDBus.CapNhatMatHangSauKhiThanhToan(HD.StrMaHoaDon);
                            }


                            this.Close();
                        
                    }

                }
                else
                {
                    MessageBox.Show("Bạn chưa mua mặt hàng nào!", "Thông Báo");
                }
            }
            else
            {
                MessageBox.Show("Thiếu thông tin khách hàng!", "Thông Báo");
            }
        }



        
        /// <summary>
        /// Kiem tra xem con hang hay khong
        /// </summary>
        ///

        public bool DaHetHang()
        {
            dtg_DanhSachMatHang.Sort(dtg_DanhSachMatHang.Columns["MaMatHang"], ListSortDirection.Ascending);
            int Count = 0;
            bool KQ = false;
            string MaMatHangCu = "";
            MatHangBUS MHBus = new MatHangBUS();
            DataTable dt = new DataTable();

            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                string Temp = dr.Cells["MaMatHang"].Value.ToString();
                int SoLuongMua = int.Parse(dr.Cells["SoLuong"].Value.ToString());
                if (String.Compare(MaMatHangCu, Temp) == 0)
                {
                    Count += SoLuongMua;
                }
                else
                {
                    MaMatHangCu = Temp;
                    Count = SoLuongMua;
                }
                dt = MHBus.LayChiTietMatHang(MaMatHangCu);
                int SoLuongTon = 0;
                if (dt.Rows.Count != 0)
                {
                    SoLuongTon = int.Parse(dt.Rows[0]["SoLuongTon"].ToString());
                }
                if (SoLuongTon < Count)
                {
                    dr.Cells["SoLuong"].Style.ForeColor = Color.Red;
                    dr.Cells["SoLuong"].Style.SelectionForeColor = Color.Red;
                    if (!KQ)
                    {
                        KQ = true;
                    }
                }
            }
            dtg_DanhSachMatHang.Sort(dtg_DanhSachMatHang.Columns["STT"], ListSortDirection.Ascending);
            return KQ;
        }

        /// <summary>
        /// Kiem tra xem con hang hay khong
        /// </summary>
        ///
        public bool DeDenHan(string MaHD)
        {
            DateTime NgayHienTai = DateTime.Now;
            //HoaDonBUS HDBus = new HoaDonBUS();
            //DateTime NgayGiao = HDBus.LayNgayGiaoHang(MaHD);
            DateTime NgayGiao = dtp_NgayGiao.Value;
            if (DateTime.Compare(NgayHienTai,NgayGiao) < 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Kiem tra mat hang con ton tai
        /// </summary>
        ///

        public bool DaXoaHang()
        {
            dtg_DanhSachMatHang.Sort(dtg_DanhSachMatHang.Columns["MaMatHang"], ListSortDirection.Ascending);
            bool KQ = false;
            MatHangBUS MHBus = new MatHangBUS();
            DataTable dt = new DataTable();

            foreach (DataGridViewRow dr in dtg_DanhSachMatHang.Rows)
            {
                string Temp = dr.Cells["MaMatHang"].Value.ToString();
                if (!MHBus.MaMatHangDaBat(Temp))
                {
                    KQ = true;
                    break;
                }
            }
            return KQ;
        }
        
        //Kha Nguyen - Giao Dien chi tiet hoa don - 5/6/2013 - end
    }
}